package org.iabako.client.ui.form.business.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.tools.DefaultSuggestHandler;
import org.iabako.client.ui.form.business.SearchForm;
import org.iabako.client.ui.form.business.util.SearchActionHandler;
import org.iabako.client.ui.layout.field.CheckboxBoolean;
import org.iabako.client.ui.layout.field.InputSwitch;
import org.iabako.client.ui.layout.widget.customField.CustomFieldsInstance;
import org.iabako.client.ui.layout.widget.datatable.DataTable;
import org.iabako.client.ui.layout.widget.tooltip.TooltipListener;
import org.iabako.shared.entity.enumeration.ConversationStatus;
import org.iabako.shared.entity.enumeration.FinancialStatusClient;
import org.iabako.shared.iservice.business.ClientServiceAsync;
import org.iabako.shared.querydefinition.columndefinition.ClientColumnEnum;
import org.iabako.shared.querydefinition.dto.QueryOperatorEnum;

/**
 * TODO javadoc
 * Date: 24/11/13
 * Time: 18:22
 */
public class ClientSearchForm extends Composite {

    @UiTemplate("org.iabako.client.ui.form.business.SearchForm.ui.xml")
    interface SearchFormTemplate extends UiBinder<HTMLPanel, SearchForm> {
    }

    interface ClientSearchTemplate extends UiBinder<HTMLPanel, ClientSearchForm> {
    }

    private static final SearchFormTemplate binderSearchForm = GWT.create(SearchFormTemplate.class);
    private static final ClientSearchTemplate binderClientSearchForm = GWT.create(ClientSearchTemplate.class);

    private final ClientServiceAsync clientServiceAsync = ClientServiceAsync.Util.getInstance();

    @UiField
    CheckboxBoolean showClientOK;
    @UiField
    CheckboxBoolean showClientInDebt;
    @UiField
    CheckboxBoolean showClientWithFutureInstallments;
    @UiField
    CustomFieldsInstance customFieldsInstance;
    @UiField
    CheckboxBoolean showPendingConversation;
    @UiField
    HTMLPanel clientCategoriesPanel;
    @UiField
    InputSwitch showClientEnabled;

    SearchForm searchForm;

    public ClientSearchForm(DefaultSuggestHandler clientSuggestHandler, DataTable dataTable) {
        searchForm = new SearchForm() {
            @Override
            protected void addSpecificCriteria() {
                addClientCriteria();
            }

            @Override
            protected void cleanSpecificCriteria() {
                cleanClientCriteria();
            }
        };

        HTMLPanel searchFormPanel = binderSearchForm.createAndBindUi(searchForm);
        HTMLPanel clientSearchPanel = binderClientSearchForm.createAndBindUi(this);

        searchForm.addToDisclosureFormPanelContent(clientSearchPanel);
        searchForm.setProperties(clientServiceAsync, clientSuggestHandler, dataTable, "search_motor_client_principal_input_title");
        searchForm.showClientCustomFieldsCriteria(customFieldsInstance);
        searchForm.showClientCategoriesCriteria(clientCategoriesPanel);

        showClientEnabled.setValue(true);

        initWidget(searchFormPanel);

        setHandlers();
    }

    private void cleanClientCriteria() {
        showClientEnabled.setValue(true);
        showClientOK.setValue(false);
        showClientInDebt.setValue(false);
        showClientWithFutureInstallments.setValue(false);

        showPendingConversation.setValue(false);
    }

    private void addClientCriteria() {
        if (showClientEnabled.getValue()) {
            searchForm.addCriteria(ClientColumnEnum.disabled, QueryOperatorEnum.EQUALS, false);
        } else {
            searchForm.addCriteria(ClientColumnEnum.disabled, QueryOperatorEnum.EQUALS, true);
        }
        if (showClientOK.getValue()) {
            searchForm.addCriteria(ClientColumnEnum.status, QueryOperatorEnum.EQUALS, FinancialStatusClient._3_OK);
        }
        if (showClientInDebt.getValue()) {
            searchForm.addCriteria(ClientColumnEnum.status, QueryOperatorEnum.EQUALS, FinancialStatusClient._1_inDebt);
        }
        if (showClientWithFutureInstallments.getValue()) {
            searchForm.addCriteria(ClientColumnEnum.status, QueryOperatorEnum.EQUALS, FinancialStatusClient._2_withFutureInstallments);
        }

        if (showPendingConversation.getValue()) {
            searchForm.addCriteria(ClientColumnEnum.conversationStatus, QueryOperatorEnum.EQUALS, ConversationStatus.outgoing);
        }
    }

    private void setHandlers() {
        DOM.sinkEvents(showClientOK.getCheckbox().getElement(), Event.MOUSEEVENTS);
        DOM.sinkEvents(showClientInDebt.getCheckbox().getElement(), Event.MOUSEEVENTS);
        DOM.sinkEvents(showClientWithFutureInstallments.getCheckbox().getElement(), Event.MOUSEEVENTS);

        TooltipListener showSalesOKLabelListener = new TooltipListener(FinancialStatusClient._3_OK.getTitle());
        showClientOK.getCheckbox().addHandler(showSalesOKLabelListener, MouseOverEvent.getType());
        showClientOK.getCheckbox().addHandler(showSalesOKLabelListener, MouseOutEvent.getType());

        TooltipListener showSalesInDebtLabelListener = new TooltipListener(FinancialStatusClient._1_inDebt.getTitle());
        showClientInDebt.getCheckbox().addHandler(showSalesInDebtLabelListener, MouseOverEvent.getType());
        showClientInDebt.getCheckbox().addHandler(showSalesInDebtLabelListener, MouseOutEvent.getType());

        TooltipListener showSalesWithFutureInstallmentsLabelListener = new TooltipListener(FinancialStatusClient._2_withFutureInstallments.getTitle());
        showClientWithFutureInstallments.getCheckbox().addHandler(showSalesWithFutureInstallmentsLabelListener, MouseOverEvent.getType());
        showClientWithFutureInstallments.getCheckbox().addHandler(showSalesWithFutureInstallmentsLabelListener, MouseOutEvent.getType());

        showClientOK.getCheckbox().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (showClientOK.getValue()) {
                    showClientInDebt.setValue(false);
                    showClientWithFutureInstallments.setValue(false);
                }
            }
        });
        showClientInDebt.getCheckbox().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (showClientInDebt.getValue()) {
                    showClientOK.setValue(false);
                    showClientWithFutureInstallments.setValue(false);
                }
            }
        });
        showClientWithFutureInstallments.getCheckbox().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (showClientWithFutureInstallments.getValue()) {
                    showClientOK.setValue(false);
                    showClientInDebt.setValue(false);
                }
            }
        });

        SearchActionHandler searchActionHandler = new SearchActionHandler(searchForm);

        showClientEnabled.addValueChangeHandler(searchActionHandler);

        showClientOK.getCheckbox().addClickHandler(searchActionHandler);
        showClientInDebt.getCheckbox().addClickHandler(searchActionHandler);
        showClientWithFutureInstallments.getCheckbox().addClickHandler(searchActionHandler);

        showPendingConversation.getCheckbox().addClickHandler(searchActionHandler);
    }

    public void setPastInstallmentsCriteria() {
        searchForm.disclosureFormPanel.setOpen(true);
        showClientInDebt.setValue(true);
        searchForm.addCriteria(ClientColumnEnum.status, QueryOperatorEnum.EQUALS, FinancialStatusClient._1_inDebt);
    }

    public void search() {
        searchForm.search();
    }
}
